﻿Imports System.Linq
Partial Class App_HashtagsMonitor
    Inherits MyUI.PageBaseApp

    Public CurrAccount As mDB.Account

    'Public WithEvents GroupConsoleSearch As UI.GroupButton(Of LinkButton)
    Public WithEvents GroupConsoleDay As UI.GroupButton(Of LinkButton)

    'Protected SortGridQuery, SortGridFollStaus As UI.SortControl

#Region "PAGE"

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        CurrAccount = CurrUser.CurrAccount

        'GroupConsoleSearch = New UI.GroupButton(Of LinkButton)(Me.ViewState, "CONSOLE_SEARCH", "bottone-private-a")
        GroupConsoleDay = New UI.GroupButton(Of LinkButton)(Me.ViewState, "CONSOLE_DAY", "pg-count-h")

        If Not Me.IsPostBack Then
            SetDataPage()
        End If

    End Sub

    Private Sub SetDataPage()

        'SetListAccount()

        If CurrAccount IsNot Nothing Then
            SetDataConsole()
            'SetDataAccountFilter()
            SetDataAccountList()
        End If

    End Sub

#End Region

#Region "CONSOLE"

    Private Sub SetDataConsole()


        Dim hashtag = CurrAccount.Tab_Query_Listen.ToDataTable ' CurrAccount.Tab_Listen_Query.REL.Listen_Query.Query.SetSelect("query.*").ToDataTable
        UI.List.Bind(Me.DdlHashtag, hashtag, "query", "id", "", False)

        If Not Me.IsPostBack Then
            'Me.GroupConsoleSearch.SelectButton = Me.RbtSearchLink
            Me.GroupConsoleDay.SelectButton = Me.RbtDayAll
        End If



    End Sub
    'Private Sub SetDataAccountFilter()

    '    Dim f = CurrAccount.Tab_Account_Filter.ToDataTable
    '    UI.List.Bind(Me.DdlAccountFilter, f, "description", "id", "", False)
    '    Me.DdlAccountFilter.Items.Insert(0, New ListItem("-- Select  --", "0"))

    '    UI.List.Bind(Me.DdlOnLineList, CurrAccount.Tab_List.ToDataTable, "fullname", "id", "", False)



    'End Sub
    'Protected Sub BtnAddList_Click(sender As Object, e As EventArgs) Handles BtnAddList.Click

    '    Dim accs = CurrAccount.View_AccountLink_ByFilter(Me.GetDataFilter).
    '        SetSelect("account.account_code").
    '        Limit(1000).
    '    ToList(Of String)("account_code")

    '    Dim tw As New MyApp.Network.Twitter(CurrAccount.GetOauth)
    '    tw.AddList(New mDB.List With {.Fullname = Me.TxtNewList.Text}, accs)


    'End Sub

    'Protected Sub GroupConsoleSearch_onselected(value As String) Handles GroupConsoleSearch.Selected
    '    SetDataAccountList()
    'End Sub


#End Region

    '#Region "FILTER"

    '    Protected Sub BtnAddFilter_Click(sender As Object, e As EventArgs) Handles BtnAddFilter.Click

    '        Dim f = New mDB.Account_Filter With {
    '            .Account_Id = CurrAccount.Id, .Description = Me.TxtNewFilter.Text
    '        }

    '        f.Filter = Me.GetDataFilter

    '        DB.InsertOnSubmit(f)
    '        DB.SubmitChanges()

    '        'f.SaveFilter_Data()

    '        SetDataAccountFilter()

    '    End Sub
    '    Protected Sub BtnRemoveFilter_Click(sender As Object, e As EventArgs) Handles BtnRemoveFilter.Click

    '        CurrAccount.Tab_Account_Filter.ByPK(Me.DdlAccountFilter.SelectedValue).Delete.ExecuteCommand()

    '        SetDataAccountFilter()
    '    End Sub
    '    Protected Sub BtnSaveFilter_Click(sender As Object, e As EventArgs) Handles BtnSaveFilter.Click

    '        Dim f = CurrAccount.Tab_Account_Filter.GetByPK(Me.DdlAccountFilter.SelectedValue)

    '        f.Filter = Me.GetDataFilter
    '        DB.SubmitChanges()

    '        SetDataAccountFilter()

    '    End Sub
    '    Protected Sub DdlAccountFilter_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DdlAccountFilter.SelectedIndexChanged
    '        Dim f = CurrAccount.Tab_Account_Filter.GetByPK(Me.DdlAccountFilter.SelectedValue)

    '        If f Is Nothing Then
    '            f = New mDB.Account_Filter
    '        End If

    '        SetDataFilter(f.Filter)

    '    End Sub
    '    Private Sub SetDataFilter(f As mDB.Account.Filter)

    '        Me.TxtUsername.Text = f.Username
    '        Me.TxtFullname.Text = f.Fullname
    '        Me.TxtBeginDate.Text = MyUI.Print.DateInput(f.FromDate)
    '        Me.TxtEndDate.Text = MyUI.Print.DateInput(f.ToDate)
    '        UI.List.SetSelectValues(Me.DdlLinkType, f.IdsLink)
    '        UI.List.SetSelectValues(Me.DdlBusiness, f.IdsBusin)
    '        UI.List.SetSelectValues(Me.DdlStatus, f.IdsStatus)
    '        UI.List.SetSelectValues(Me.DdlCategories, f.IdsCat)
    '        UI.List.SetSelectValues(Me.DdlGender, f.IdsGend)
    '        UI.List.SetSelectValues(Me.DdlLanguageNot, f.IdsLangNot)
    '        UI.List.SetSelectValues(Me.DdlLanguage, f.IdsLang)

    '    End Sub
    '    Private Function GetDataFilter() As mDB.Account.Filter

    '        Return New mDB.Account.Filter With {
    '            .FromDate = Utils.Conv.ToDate(Me.TxtBeginDate.Text), .ToDate = Utils.Conv.ToDate(Me.TxtEndDate.Text),
    '            .Username = Me.TxtUsername.Text, .Fullname = Me.TxtFullname.Text,
    '            .IdsLink = UI.List.GetSelectValuesInt(Me.DdlLinkType),
    '            .IdsBusin = UI.List.GetSelectValuesInt(Me.DdlBusiness),
    '            .IdsStatus = UI.List.GetSelectValuesInt(Me.DdlStatus),
    '            .IdsCat = UI.List.GetSelectValuesInt(Me.DdlCategories),
    '            .IdsGend = UI.List.GetSelectValuesInt(Me.DdlGender),
    '            .IdsLangNot = UI.List.GetSelectValues(Me.DdlLanguageNot),
    '            .IdsLang = UI.List.GetSelectValues(Me.DdlLanguage)
    '        }
    '    End Function

    '#End Region

#Region "ACCOUNT LIST"
    Protected Enum OrderByType
        NONE = 0
        FOLLOWER
        RANK
    End Enum


    Protected Sub SetDataAccountList()



        '        Me.LblUserFound.Text = "-"

        Dim FromDate = Utils.Conv.ToDate(Me.TxtBeginDate.Text)
        Dim ToDate = Utils.Conv.ToDate(Me.TxtEndDate.Text)

        If FromDate = Date.MinValue Then
            FromDate = Now.AddDays(-30).Date
        End If
        If ToDate = Date.MinValue Then
            ToDate = Now.Date
        End If


        Me.TxtFullname.Enabled = True
        Me.TxtFullname.Enabled = False

        Me.PnlSearchHashtag.Visible = True
        Me.PnlAccountListen.Visible = True


        Dim qQueries = CurrAccount.Tab_Query_Listen     'CurrUser.Tab_Query.BY.Query.Real_Time(1)
        Dim ids = UI.List.GetSelectValuesInt(Me.DdlHashtag) '   (From i In Me.DdlHashtag.GetSelectedIndices Select CInt(Me.DdlHashtag.Items(i).Value)).ToList
        If ids.Count <> 0 Then
            qQueries.Where("query.id", "IN", ids)
        End If
        'Dim queries = qQueries.ToList

        Dim gruppo = New mDB.Group_Query(qQueries.ToList)

        Dim dt As DataTable = Nothing

        If gruppo.QueryList.Count > 0 Then

            dt = gruppo.View_Account_Search(FromDate, ToDate, Me.TxtUsername.Text).
                GroupBy("account_code").OrderBy("Post_count desc").
                SetSelect("account_username", "account_code", "sum(Post_count) as Post_count", "account_follower_count").
                Limit(1000).
            ToDataTable()

            '-------------------------------------------------------
            ' LAYOUT
            '-------------------------------------------------------

            'UI.Grid.Bind(Me.GridAccount, dt)

            Dim dtList = New List(Of List(Of Object))

            Dim TotRows = 20
            Dim RowCont = 0

            For ct = 1 To Math.Ceiling(dt.Rows.Count / (TotRows * 3))
                For ct2 = 1 To TotRows
                    Dim r = New List(Of Object)
                    For ct3 = 0 To 2
                        If dt.Rows.Count > RowCont + (TotRows * ct3) Then
                            r.Add(dt.Rows(RowCont + (TotRows * ct3)))
                        Else
                            r.Add(dt.NewRow)
                        End If
                    Next

                    RowCont += 1
                    dtList.Add(r)
                Next
                RowCont += TotRows + TotRows
            Next

            UI.ListData.Bind(Me.ListAccountListen, dtList)

            '-------------------------------------
            ' CHART

            'Dim t1 = mDB._Query_Account.GetTrendAccount(queries, FromDate, ToDate)
            Dim t1 = gruppo.GetTrendAccount(FromDate, ToDate)

            Dim DataChart As New List(Of Object())
            DataChart.Add({"data", "post", "impression"})
            For ct = 0 To t1.Count - 1
                DataChart.Add({MyUI.Print.DateMonthDay(t1(ct).Post_Update), t1(ct).Post_Count, t1(ct).Account_Follower_Count})
            Next

            Dim json = MyApp.JSONSerialize(New With {
                .data = DataChart
            })

            Me.AddJScript(SF("DrawChartPostSend({0});", json))

        End If


        Me.UpPAccountListen.Update()


    End Sub

    Protected Function GetImageGenderType(b As mDB.Account.BusinessType, g As mDB.Gender_Name.Type) As String

        If b = GloukDB.Account.BusinessType.BUSINESS Then
            Return "business.png"
        Else
            If g = GloukDB.Gender_Name.Type.MALE Then
                Return "man.png"
            End If
            If g = GloukDB.Gender_Name.Type.FEMALE Then
                Return "woman.png"
            End If

        End If

        Return ""
    End Function
    Protected Function GetImageStatus(b As mDB.Account.StatusType) As String

        Select Case b
            Case GloukDB.Account.StatusType.FAKE
                Return "conn-fake.png"
            Case GloukDB.Account.StatusType.INACTIVE
                Return "conn-inactive.png"
            Case GloukDB.Account.StatusType.LOW_ACTIVITY, GloukDB.Account.StatusType.MEDIUM_ACTIVITY, GloukDB.Account.StatusType.HIGHT_ACTIVITY
                Return "conn-good.png"
            Case Else
                Return ""
        End Select

    End Function

    Protected Sub ChkExcluded_CheckedChanged(sender As Object, e As EventArgs)

        Dim chk As CheckBox = sender
        Dim row = UI.ListDataItem.GetRow(sender)
        Dim id = UI.ListDataItem.GetKey(row)

        'Dim link = CurrAccount.Tab_Account_Link(GloukDB.Account_Link.Type.FOLLOWER, id).ToObj
        Dim link = CurrAccount._Tab_Account_Link(mDB._Account_Link.Type.FOLLOWER, id).ToObj
        If link IsNot Nothing Then
            link.Excluded = chk.Checked

            DB.SubmitChanges()
        End If




    End Sub

    Protected Sub BtnExport_Click(sender As Object, e As EventArgs) Handles BtnExport.Click

        SetDataAccountList()

    End Sub


#End Region


End Class







